//
//  TrackMapViewController.m
//  Teximeter
//
//  Created by BEA DONG HWAN on 11. 2. 5..
//  Copyright 2011 Ajou University. All rights reserved.
//

#import "TrackMapViewController.h"
#import "GPSModel.h"
#import "Teximeter_coreAppDelegate.h"
#import "track.h"
@implementation TrackMapViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

-(IBAction)clickConfirm
{
	[self dismissModalViewControllerAnimated:YES];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	CGRect mapFrame = self.view.bounds;
	mapFrame.origin.y +=40;
	mapFrame.size.height-=40;
	mapView = [[MKMapView alloc]initWithFrame:mapFrame];
	
	mapView.delegate =self;
	dataArray = [[NSMutableArray alloc]init];
	[self.view addSubview:mapView];
//	[mapView setShowsUserLocation:YES];
	[self insertAnnotation];
	
}


-(void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
}
-(void)insertAnnotation
{

	SharedDBManager* db = [SharedDBManager sharedInstance];
	[dataArray setArray:[db selecAll]];
	NSLog(@"datacount = %d",[dataArray count]);
	
	double middleLati = 0;
	double middleLong = 0;
	double disLati =0;
	double disLong =0;

	for(int i = 0;i<[dataArray count];i++)
	{
		track* aData = [dataArray objectAtIndex:i];
		CLLocationCoordinate2D curLocation;
		curLocation.latitude = [aData.latitude doubleValue];
		curLocation.longitude = [aData.longitude doubleValue];
		AddressAnnotation* ano = [[AddressAnnotation alloc]initWithCoordinate:curLocation];
		[mapView addAnnotation:ano];
		
		if(i==0)
		{
			middleLati = [aData.latitude doubleValue];
			middleLong = [aData.longitude doubleValue];
		}
		if(i==[dataArray count]-1)
		{
			disLati = middleLati-[aData.latitude doubleValue];
			disLong = middleLong-[aData.longitude doubleValue];
			
			middleLati += [aData.latitude doubleValue];
			middleLong += [aData.longitude doubleValue];
		}
	}
	
	if([dataArray count]>=2)
	{
		//Set Zoom level using Span
		MKCoordinateSpan span;
		
		if(disLati < 0)
			disLati*=-1;
		if(disLong <0)
			disLong*=-1;
		
		span.latitudeDelta=disLati*1.2;
		span.longitudeDelta=disLong*1.2;
		
		CLLocationCoordinate2D location;
		location.latitude = middleLati/2.0f;
		location.longitude = middleLong/2.0f;
		
		region.span = span;
		region.center = location;
		[mapView setRegion:region];
	}
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[dataArray release];
    [super dealloc];
}






@end
